namespace AgentJohnson.Options
{
  using System;
  using System.Windows.Forms;
  using AgentJohnson.CodeAnnotations;
  using JetBrains.UI.CrossFramework;
  using JetBrains.UI.Options;

  /// <summary>The value analysis options page.</summary>
  [OptionsPage(PageName, "Code Annotations", "AgentJohnson.Resources.Assertions.gif", ParentId = AgentJohnsonPage.OptionPageName)]
  public partial class CodeAnnotationsOptionsPage : UserControl, IOptionsPage
  {
    #region Constants and Fields

    /// <summary>
    /// The page name.
    /// </summary>
    private const string PageName = "AgentJohnson.CodeAnnotationsPage";

    /// <summary>
    /// The _instance.
    /// </summary>
    private static CodeAnnotationsOptionsPage instance;

    #endregion

    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="CodeAnnotationsOptionsPage"/> class.
    /// </summary>
    public CodeAnnotationsOptionsPage()
    {
      this.InitializeComponent();
      instance = this;
    }

    #endregion

    #region Implemented Interfaces

    #region IOptionsPage

    /// <summary>
    /// Gets the control to be shown as page.
    /// </summary>
    /// <remarks>May be <c>Null</c> if the page does not have any UI.</remarks>
    /// <value></value>
    public EitherControl Control
    {
      get
      {
        return this;
      }
    }

    /// <summary>
    /// Gets the ID of this option page.
    /// <see cref="T:JetBrains.UI.Options.IOptionsDialog"/> or <see cref="T:JetBrains.UI.Options.OptionsPageDescriptor"/> could be used to retrieve the <see cref="T:JetBrains.UI.Options.OptionsManager"/> out of it.
    /// </summary>
    /// <value></value>
    public string Id
    {
      get
      {
        return PageName;
      }
    }

    #endregion

    #endregion

    #region Properties

    /// <summary>
    /// Gets the instance.
    /// </summary>
    /// <value>The instance.</value>
    public static CodeAnnotationsOptionsPage Instance
    {
      get
      {
        return instance;
      }
    }

    #endregion

    #region Public Methods

    /// <summary>Commits this instance.</summary>
    public void Commit()
    {
      CodeAnnotationSettings.Instance.PublicAssertion = this.PublicAssertion.Text;
      CodeAnnotationSettings.Instance.NonPublicAssertion = this.NonPublicAssertion.Text;
    }

    /// <summary>Displays this instance.</summary>
    public void Display()
    {
      this.PublicAssertion.Text = CodeAnnotationSettings.Instance.PublicAssertion;
      this.NonPublicAssertion.Text = CodeAnnotationSettings.Instance.NonPublicAssertion;
    }

    #endregion

    #region Implemented Interfaces

    #region IOptionsPage

    /// <summary>Invoked when OK button in the options dialog is pressed
    /// If the page returns <c>false</c>, the the options dialog won't be closed, and focus
    /// will be put into this page</summary>
    /// <returns><c>True</c>, if OK.</returns>
    public bool OnOk()
    {
      this.Commit();

      return true;
    }

    /// <summary>Check if the settings on the page are consistent, and page could be closed</summary>
    /// <returns><c>true</c> if page data is consistent</returns>
    public bool ValidatePage()
    {
      return true;
    }

    #endregion

    #endregion

    #region Methods

    /// <summary>Raises the <see cref="E:System.Windows.Forms.UserControl.Load"/> event.</summary>
    /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data. </param>
    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);

      this.Display();
    }

    #endregion
  }
}